// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   DialChart.java

package org.achartengine.chart;

import android.graphics.Canvas;
import android.graphics.Paint;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DialRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

// Referenced classes of package org.achartengine.chart:
//			RoundChart

public class DialChart extends RoundChart
{

	private static final int NEEDLE_RADIUS = 10;
	private DialRenderer mRenderer;

	public DialChart(CategorySeries categoryseries, DialRenderer dialrenderer)
	{
		super(categoryseries, dialrenderer);
		mRenderer = dialrenderer;
	}

	public void draw(Canvas canvas, int i, int j, int k, int l, Paint paint)
	{
		paint.setAntiAlias(mRenderer.isAntialiasing());
		paint.setStyle(android.graphics.Paint.Style.FILL);
		paint.setTextSize(mRenderer.getLabelsTextSize());
		int i1 = getLegendSize(mRenderer, l / 5, 0.0F);
		int j1 = i;
		int k1 = j;
		int l1 = i + k;
		int i2 = mDataset.getItemCount();
		String as[] = new String[i2];
		for (int j2 = 0; j2 < i2; j2++)
			as[j2] = mDataset.getCategory(j2);

		if (mRenderer.isFitLegend())
			i1 = drawLegend(canvas, mRenderer, as, j1, l1, j, k, l, i1, paint, true);
		int k2 = (j + l) - i1;
		drawBackground(mRenderer, canvas, i, j, k, l, paint, false, 0);
		int l2 = Math.min(Math.abs(l1 - j1), Math.abs(k2 - k1));
		int i3 = (int)((double)l2 * 0.34999999999999998D * (double)mRenderer.getScale());
		if (mCenterX == 0x7fffffff)
			mCenterX = (j1 + l1) / 2;
		if (mCenterY == 0x7fffffff)
			mCenterY = (k2 + k1) / 2;
		float f = (float)i3 * 0.9F;
		float f1 = (float)i3 * 1.1F;
		double d = mRenderer.getMinValue();
		double d1 = mRenderer.getMaxValue();
		double d2 = mRenderer.getAngleMin();
		double d3 = mRenderer.getAngleMax();
		if (!mRenderer.isMinValueSet() || !mRenderer.isMaxValueSet())
		{
			int j3 = mRenderer.getSeriesRendererCount();
			for (int k3 = 0; k3 < j3; k3++)
			{
				double d5 = mDataset.getValue(k3);
				if (!mRenderer.isMinValueSet())
					d = Math.min(d, d5);
				if (!mRenderer.isMaxValueSet())
					d1 = Math.max(d1, d5);
			}

		}
		if (d == d1)
		{
			d *= 0.5D;
			d1 *= 1.5D;
		}
		paint.setColor(mRenderer.getLabelsColor());
		double d4 = mRenderer.getMinorTicksSpacing();
		double d6 = mRenderer.getMajorTicksSpacing();
		if (d4 == 1.7976931348623157E+308D)
			d4 = (d1 - d) / 30D;
		if (d6 == 1.7976931348623157E+308D)
			d6 = (d1 - d) / 10D;
		drawTicks(canvas, d, d1, d2, d3, mCenterX, mCenterY, f1, i3, d4, paint, false);
		drawTicks(canvas, d, d1, d2, d3, mCenterX, mCenterY, f1, f, d6, paint, true);
		int l3 = mRenderer.getSeriesRendererCount();
		for (int i4 = 0; i4 < l3; i4++)
		{
			double d7 = getAngleForValue(mDataset.getValue(i4), d2, d3, d, d1);
			paint.setColor(mRenderer.getSeriesRendererAt(i4).getColor());
			boolean flag = mRenderer.getVisualTypeForIndex(i4) == org.achartengine.renderer.DialRenderer.Type.ARROW;
			drawNeedle(canvas, d7, mCenterX, mCenterY, f, flag, paint);
		}

		drawLegend(canvas, mRenderer, as, j1, l1, j, k, l, i1, paint, false);
		drawTitle(canvas, i, j, k, paint);
	}

	private double getAngleForValue(double d, double d1, double d2, double d3, double d4)
	{
		double d5 = d2 - d1;
		double d6 = d4 - d3;
		return Math.toRadians(d1 + ((d - d3) * d5) / d6);
	}

	private void drawTicks(Canvas canvas, double d, double d1, double d2, 
			double d3, int i, int j, double d4, double d5, double d6, Paint paint, boolean flag)
	{
		for (double d7 = d; d7 <= d1; d7 += d6)
		{
			double d8 = getAngleForValue(d7, d2, d3, d, d1);
			double d9 = Math.sin(d8);
			double d10 = Math.cos(d8);
			int k = Math.round((float)i + (float)(d5 * d9));
			int l = Math.round((float)j + (float)(d5 * d10));
			int i1 = Math.round((float)i + (float)(d4 * d9));
			int j1 = Math.round((float)j + (float)(d4 * d10));
			canvas.drawLine(k, l, i1, j1, paint);
			if (!flag)
				continue;
			paint.setTextAlign(android.graphics.Paint.Align.LEFT);
			if (k <= i1)
				paint.setTextAlign(android.graphics.Paint.Align.RIGHT);
			String s = (new StringBuilder()).append(d7).append("").toString();
			if (Math.round(d7) == (long)d7)
				s = (new StringBuilder()).append((long)d7).append("").toString();
			canvas.drawText(s, k, l, paint);
		}

	}

	private void drawNeedle(Canvas canvas, double d, int i, int j, double d1, 
			boolean flag, Paint paint)
	{
		double d2 = Math.toRadians(90D);
		int k = (int)(10D * Math.sin(d - d2));
		int l = (int)(10D * Math.cos(d - d2));
		int i1 = (int)(d1 * Math.sin(d));
		int j1 = (int)(d1 * Math.cos(d));
		int k1 = i + i1;
		int l1 = j + j1;
		float af[];
		if (flag)
		{
			int i2 = i + (int)(d1 * 0.84999999999999998D * Math.sin(d));
			int j2 = j + (int)(d1 * 0.84999999999999998D * Math.cos(d));
			af = (new float[] {
				(float)(i2 - k), (float)(j2 - l), (float)k1, (float)l1, (float)(i2 + k), (float)(j2 + l)
			});
			float f = paint.getStrokeWidth();
			paint.setStrokeWidth(5F);
			canvas.drawLine(i, j, k1, l1, paint);
			paint.setStrokeWidth(f);
		} else
		{
			af = (new float[] {
				(float)(i - k), (float)(j - l), (float)k1, (float)l1, (float)(i + k), (float)(j + l)
			});
		}
		drawPath(canvas, af, paint, true);
	}
}
